package cn.antia.cep.right;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import cn.antia.cep.common.AntiaLog;

public class TreeBuilder {
	/**
	 * 给定资源集合构造权限树
	 *
     * @param resources 资源集合
     * @return 权限树*/
	public RightTree buildTree(List resources){
		TreeNode resource; //一个资源
		RightTree rightTree = new RightTree(); //权限树
		//int maxDeep = 0; //最大层次
		HashMap resourceMap = new HashMap(); //存储父节点ID与子节点的LIST
		
		//确定根结点
    	int min = 10;
    	int posi = 10;
    	 for (int i = 0;i<resources.size();i++) {
    		 resource = (TreeNode) resources.get(i);
    		 if(resource.getDeep().intValue() < min){
    			 min = resource.getDeep().intValue();
    			 posi = i;
    		 }
    	 }
    	 TreeNode root = (TreeNode)resources.get(posi);

        for (Iterator it = resources.iterator(); it.hasNext();) {
            Object resource1 = it.next();
            resource = (TreeNode) resource1;
            if (resource.getId() == root.getId()) {
                //根结点
                rightTree.setNode(resource);
            } else {
                Integer parentId = resource.getParentId();
                List childNodes = (List) resourceMap.get(parentId);
                if (childNodes == null) {
                    childNodes = new ArrayList();
                    childNodes.add(resource);
                    resourceMap.put(parentId, childNodes);
                } else {
                    childNodes.add(resource);
                }
            }
        }
        if(rightTree.getNode() == null){
			AntiaLog.error("构造权限树失败!没有取到根结点。");
			return null;
		}
		//递归构建
		rightTree = recursiveBuild(rightTree,resourceMap);
		return rightTree;
	}
	
	/**
	 * 递归构建资源树
	 *
     * @param root  树根
     * @param map 存放 parentId - > childs
     * @return 完整的资源树*/
	private RightTree recursiveBuild(RightTree root, HashMap map){
		Integer parentId = root.getNode().getId();
		List childs = (List) map.get(parentId);
		if(childs != null && childs.size() > 0){
			List rights = new ArrayList();
            for (Iterator it = childs.iterator(); it.hasNext();) {
                TreeNode child = (TreeNode) it.next();
                RightTree rightTree = new RightTree();
                rightTree.setNode(child);
                rights.add(rightTree);
                recursiveBuild(rightTree, map);
            }
            root.setChild(rights);
		}else{
			return root;
		}
		return root;
	}
	
	/**
     * 取得某一个资源下的子资源树
     * @param rightTree rightTree
     * @param resourceId parent resource id(root resource id)
     * @return RightTree
     */
	public RightTree cutTree(RightTree rightTree,int resourceId){
		List list = new ArrayList();
		list = cutList(rightTree,resourceId,list);
		rightTree = this.buildTree(list);
		return rightTree;
	}

	private List cutList(RightTree rightTree,int resourceId,List list){
		if(rightTree.hasChild()){
			TreeNode node = rightTree.getNode();
			if(node.getId().intValue() == resourceId){
				list.add(node);
				List childs = rightTree.getChild();
				list.addAll(childs);
				if(childs != null && childs.size() > 0){
					for(int i = 0;i<childs.size();i++){
						int rid = ((RightTree)childs.get(i)).getNode().getId().intValue();
						cutList((RightTree)childs.get(i),rid,list);
					}
				}
			}
        }
		return list;
	}
	
	 /**
     * 根据资源列表与批定的根资源ID，构造权限树
     * @param resources resource list
     * @param rootId root resource id
     * @return RightTree
     */
	public RightTree getRightTreeByRoot(List resources,int rootId){
		TreeNode resource; //一个资源
		TreeNode root = null;
		RightTree rightTree = new RightTree(); //权限树
		HashMap resourceMap = new HashMap(); //存储父节点ID与子节点的LIST
		
    	 for (int i = 0;i<resources.size();i++) {
    		 resource = (TreeNode) resources.get(i);
    		 if(resource.getId().intValue() == rootId){
    			 root = resource;
    			 rightTree.setNode(root);
    		 }
    	 }
    	 if(root == null){
    		AntiaLog.error("没有找到指定根ID的根结点。根ID：" + rootId);
    		return null;
    	 }

        for (Iterator it = resources.iterator(); it.hasNext();) {
            resource = (TreeNode) it.next();
            if (resource.getId().intValue() == rootId) {
                //根结点
                rightTree.setNode(resource);
            } else {
                Integer parentId = resource.getParentId();
                List childNodes = (List) resourceMap.get(parentId);
                if (childNodes == null) {
                    childNodes = new ArrayList();
                    childNodes.add(resource);
                    resourceMap.put(parentId, childNodes);
                } else {
                    childNodes.add(resource);
                }
            }
        }
        if(rightTree.getNode() == null){
			AntiaLog.error("构造权限树失败!没有取到根结点。");
			return null;
		}
		//递归构建
		rightTree = recursiveBuild(rightTree,resourceMap);
		return rightTree;
	}

}
